﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
using AudiPyme.DAL;

namespace AudiPyme.UI
{
    public partial class AjaxPrueba : System.Web.UI.Page, ICallbackEventHandler
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(" function llamarServidor(arg) { ");
            sb.Append(ClientScript.GetCallbackEventReference(this, "arg", "Obtienedatos", "null", false));
            sb.Append("};");

            this.Page.ClientScript.RegisterClientScriptBlock(System.Type.GetType("System.String"), "llamarServidor", sb.ToString(), true);
            DropDownList1.Attributes.Add("onchange", "llamarServidor(document.getElementById('" + DropDownList1.ClientID + "').value);return false;");

            if (!IsPostBack)
            {
                Cargar_Tipo_Persona();
            }
        }

        private string _callbackArgument;
        public string GetCallbackResult()
        {
            return _callbackArgument;
        }
        public void RaiseCallbackEvent(string eventArgument)
        {
            try
            {
                if (eventArgument != string.Empty)
                {
                    //cargra grilla
                    if (Convert.ToString(eventArgument) == "--seleccionar--")
                    {
                        GVControles.DataBind();
                    }
                    else
                    {
                        int ObjControl;
                        int.TryParse(eventArgument, out ObjControl);
                        ModeloContainer agenteBD = new ModeloContainer();
                        var ControlesQuery = from ControlesObj in agenteBD.ControlesEstablecer
                                     where ControlesObj.IdObjControl == ObjControl
                                     select new { ControlesObj.IdControl, ControlesObj.Nombre, ControlesObj.Descripcion };
                        GVControles.DataSource = ControlesQuery;
                        DataBind();
                    }

                    StringWriter sr = new StringWriter();
                    HtmlTextWriter htm = new HtmlTextWriter(sr);

                    GVControles.RenderControl(htm);

                    htm.Flush();

                    _callbackArgument = sr.ToString();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        public void Cargar_Tipo_Persona()
        {
            try
            {
                ModeloContainer agenteBD = new ModeloContainer();
                var CustQuery = from cust in agenteBD.ObjetivosControlEstablecer
                                select new { cust.IdObjControl };
                DropDownList1.DataValueField = "IdObjControl";
                DropDownList1.DataTextField = "IdObjControl";
                DropDownList1.DataSource = CustQuery;
                DataBind();
                DropDownList1.Items.Insert(0, "--seleccionar--");
            }
            catch (Exception)
            { }
        }


    }
}